package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.UploadUtils;
import com.cloudxingkong.dianyinke.NewTeacher.bean.VideoDetail;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.cloudxingkong.dianyinke.VideoPlay.SuperVideoPlay;
import com.cloudxingkong.dianyinke.VideoPlay.VideoPlay;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import de.hdodenhof.circleimageview.CircleImageView;

public class UVDetail extends AppCompatActivity {

    private ImageView ivCover;
    private CircleImageView ivAvatar;
    private TextView tvMemname,tvTitle,tvViews,tvLikes,tvNote;
    private String memname,title,views,likes,note,vID,vPath;
    private SharedPreferences sharedPreferences;
    private String vAvatar;
    private String vCover;
    private String sessionID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uvdetail);
        init();
    }

    private void init() {
        ivCover = (ImageView) findViewById(R.id.uv_detail_cover);
        ivAvatar = (CircleImageView) findViewById(R.id.uv_detail_avator);
        tvMemname = (TextView) findViewById(R.id.uv_detail_teachername);
        tvTitle = (TextView) findViewById(R.id.uv_detail_title);
        tvViews = (TextView) findViewById(R.id.uv_detail_bofangtimes);
        tvLikes = (TextView) findViewById(R.id.uv_detail_commend);
        tvNote = (TextView) findViewById(R.id.uv_detail_info);

        Intent intent = getIntent();
        title=intent.getStringExtra("vTitle");
        memname=intent.getStringExtra("vMemname");
        views=intent.getStringExtra("vViews");
        likes=intent.getStringExtra("vLikes");
        note=intent.getStringExtra("vNote");
        vID = intent.getStringExtra("vID");
        vPath = intent.getStringExtra("vPath");
        vAvatar = intent.getStringExtra("vAvatar");
        vCover = intent.getStringExtra("vCover");
        tvLikes.setText(likes);
        tvMemname.setText(memname);
        tvNote.setText(note);
        tvTitle.setText(title);
        tvViews.setText(views);
        if (vAvatar!=null&&!vAvatar.equals("")){
            ImageLoader.getInstance().displayImage(vAvatar,ivAvatar);
        }
        if (vCover != null && !vCover.equals("")) {
            ImageLoader.getInstance().displayImage(vCover,ivCover);
        }
        sharedPreferences = getSharedPreferences("data_dyk", 0);
        sessionID = CookieHelper.getSessionID(sharedPreferences);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("vTitle",title);
        edit.putString("vNote", note);
        edit.putString("vID",vID);
        edit.commit();

    }

    public void back(View view) {
        finish();
    }

    public void playVideo(View view) {
        Intent intent = new Intent(this, SuperVideoPlay.class);
        intent.putExtra("videoPath",vPath);
        intent.putExtra("videoID", vID);
        startActivityForResult(intent, 1020);
    }

    public void updataLesson(View view) {
        Intent intent = new Intent(this, UploadingVideo.class);
        intent.putExtra("updateVideo","102");
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1020) {
            if (resultCode == 10086) {
                loadData();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void loadData() {
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.VIDEO_DETAIL);
        params.addHeader("cookie", sessionID);
        params.addBodyParameter("id", vID);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "视频详情 \n" + result);
                VideoDetail videoDetail = (new Gson()).fromJson(result, VideoDetail.class);
                if (videoDetail.isSuccess()) {
                    VideoDetail.ObjBean obj = videoDetail.getObj();
                    int views = obj.getViews();
                    int likes = obj.getLikes();
                    tvLikes.setText(likes + "");
                    tvViews.setText(views + "");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
